package com.cmwa.solution.business.product.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.product.entity.TreNavBizVo;
import com.cmwa.solution.business.product.service.TreNavBizService;


@Controller
@RequestMapping("/business/nav")
public class TreNavBizController extends BaseController {
	
	private final static String JSP_PATH = "business/product/";
	
	@Resource
	private TreNavBizService treNavBizService;
	
	@RequestMapping("/navView")
	public String navView(ModelMap model) {
		return JSP_PATH + "treNavList";
	}

	@ResponseBody
	@RequestMapping("/treNavListPage")
	public Page<TreNavBizVo> treNavListPage(SearchParam param) {
		Page<TreNavBizVo> list = treNavBizService.treNavListPage(param);
		return list;
	}
	
	@RequestMapping("/addTreNav")
	public String addNav(Model model) {
		return JSP_PATH + "addTreNav";
	}
	
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/saveTreNav")
	public String saveTreNav(TreNavBizVo treNavBizVo) {
		treNavBizService.saveTreNav(treNavBizVo);
		return redirectSuccess();
	}
	
	/**
	 * 跳转修改页面
	 * @param navBizId
	 * @param model
	 * @return
	 */
	@RequestMapping("/updateView")
	public String updateView(@RequestParam(value="navBizId") String navBizId,ModelMap model) {
		TreNavBizVo treNavBizVo = treNavBizService.treNavDetail(navBizId);
		model.addAttribute("treNavBizVo", treNavBizVo);
		return JSP_PATH+"updateTreNav";
	}
	
	/**
	 * 修改
	 * @return
	 */
	@RequestMapping("/updateTreNav")
	public String updateTreNav(TreNavBizVo treNavBizVo) {
		treNavBizService.updateTreNav(treNavBizVo);
		return redirectSuccess();
	}
	
	/**
	 * 根据不同的参数跳转页面
	 * detail :详情页
	 * @param model
	 * @return
	 */
	@RequestMapping("/treNavDetail")
	public String treNavDetail(@RequestParam("navBizId") String navBizId,ModelMap model) {
		TreNavBizVo treNavBizVo = treNavBizService.treNavDetail(navBizId);
		model.addAttribute("treNavBizVo", treNavBizVo);
		return JSP_PATH + "treNavDetail";
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result<TreNavBizVo> delete(@RequestParam("navBizId") String navBizId) {
		Result<TreNavBizVo> result = new Result<TreNavBizVo>();
		result.setSuccess(true);
		result.setMessage("删除成功");
		try {
			treNavBizService.delTreNavById(navBizId);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("删除失败");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/isRepeat")
	public Result<TreNavBizVo> isRepeat(@RequestParam(value="navBizId",required=false)String navBizId,
			@RequestParam(value="subPrdCode")String subPrdCode,
			@RequestParam("assessmentDate") String assessmentDate) {
		Result<TreNavBizVo> result = new Result<TreNavBizVo>();
		result.setMessage("查询成功");
		try {
			result.setSuccess(!treNavBizService.isRepeat(navBizId,subPrdCode,assessmentDate));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("查询失败");
		}
		return result;
	}
}
